<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2018/5/31

 * Time: 11:41

 */



namespace App\Model;

use Illuminate\Database\Eloquent\Model;

use Auth;

class Members extends Model {

	/**

	 * 会员表

	 * @var string

	 */

	protected $table = 'members';

	protected $fillable = [

		'uid',

		'name',
		'original_id',

		'user_name',

		'password_pay',

        'salting_pay',

		'password',

		'salting',

		'emil',

		'openid',

		'phone',

		'branch_id',

		'sign_in',

		'num',

		'dnum',

		'referrer',

		'feferrer_name',

        'nodes',

        'DT_shareholder',

		'nodes_name',

		'area',

		'a',

		'b',

		'activation_time',

		'level',

        'voucher',

        'level_grade',

		'level_name',

		'credit1',

		'credit2',

		'credit3',

        'credit4',

        'credit5',

        'credit6',

		'performance1',

		'performance2',

		'type',

		'note',

		'is_withdraw',

		'is_shift',

		'ip',

		'company_id',

		'registered',

        'enroll_time',

        'grilled',

        'super_level',

        'is_credit3',

        'is_credit6',

        'super_level_name',

        'super_recommend',

        'team_member_count',

        'equity_price',

        'DT_performance',

        'equity_price2',

        'performance',

        'super_shop_isdown',

        'order_money',

        'stores_number',

        'achievement',
        'cumulative_times',
        'day_times',
        'day_probability',
        'numbers',
        'team_achievement',
        'achievement_increment',
        'single_achievement',
        'day_achievement',
        'team_numbers',
        'order_ids',
        'by_num',
        'team_profit',
        'platform_profit',
        'is_read',
        'is_out',
        'share_bonus',
        'consumption_bonus',
        'team_bonus',

	];







	/**

	 * 统计会员数量

	 * @param array $where

	 * @return int

	 */

	public function count_member($where = ['type', '>', 0]) {

        $rico = $this->where($where)->count();

        return $rico;

    }



    /**

     * 获取全部会员

     * @param array $keyword

     * @param bool $pag

     * @return bool|\Illuminate\Contracts\Pagination\LengthAwarePaginator

     */

	public function get_member( $keyword = array() ,$pag = true) {
		$table = $this->where('type' , '>=' , 0);

        //会员等级

		if(!empty($keyword['level'])){

            $table = $this->where(['level' => $keyword['level']]);

        }
        if(isset($keyword['is_read'])){

            $table = $this->where(['is_read' => $keyword['is_read']]);

        }
        if(isset($keyword['platform_profit']) ){
            $table = $this->where(['platform_profit' => $keyword['platform_profit']]);

        }
        if(!empty($keyword['phone'])){

            $table = $this->where(['phone' => $keyword['phone']]);

        }

//        //分公司
//
//        if(!empty($keyword['company'])){
//
//            $table = $this->where(['company_id' => $keyword['company']]);
//
//        }

        //时间查询

        if(!empty($keyword['endtime'])){

            if($keyword['type'] == 4){

                $table = $this->where('sign_in','>',strtotime($keyword['endtime'][0]))->where('sign_in','<',strtotime($keyword['endtime'][1]));

            } else {

                $table = $this->where('enroll_time','>',strtotime($keyword['endtime'][0]))->where('enroll_time','<',strtotime($keyword['endtime'][1]));

            }



		} else {

            if(!empty($keyword['type'])){

                if($keyword['type'] == 4){

                    $table = $this->where('sign_in','>',(time()-604800))->where('sign_in','<',time());

                }

            }

        }

        //分类状态

        if(!empty($keyword['type'])){

		    if($keyword['type'] == 1){

                $table = $this->where(['shareholder' => 1]);

            } elseif ($keyword['type'] == 2) {

                $table = $this->where(['type' => 0]);

            } elseif ($keyword['type'] == 3) {

                $table = $this->where(['registered' => 0]);

            } elseif ($keyword['type'] == 5) {

                $table = $this->where(['DT_shareholder' => 1]);

            }

        }

        //关键字

        if(!empty($keyword['keyword'])){

            $table = $this->where(['name' => $keyword['keyword']])
                ->orWhere('stores_number','like','%'.$keyword['keyword'].'%')
                ->orWhere('user_name','like','%'.$keyword['keyword'].'%')
                ->orWhere('super_level_name','like','%'.$keyword['keyword'].'%')
                ->orWhere('level_name','like','%'.$keyword['keyword'].'%');

        }

//        if($pag){

//            $member = $table->orderBy('uid','desc')->paginate(15);

//        } else {

//            $member = $table->orderBy('uid','desc')->get()->toArray();

//        }

        //当前用户

        $user = Auth::user();

            if($pag){

                if(!empty($keyword['type'])){

                    if($keyword['type'] == 4) {

                        $member = $table->orderBy('sign_in', 'desc')->paginate(15);

                    } else {

                        $member = $table->orderBy('uid','desc')->paginate(15);


                    }

                } else {

                    $member = $table->orderBy('uid','desc')->paginate(15);

                }

            } else {

                $member = $table->orderBy('uid','desc')->get()->toArray();

            }





		if ($member) {

			return $member;

		} else {

			return false;

		}

	}

	/**

	 * 获取分享人手机

	 * @param int $referrer

	 * @return bool|Model|null|static

	 */

	public function find_member($referrer = 0 ) {

		$member = $this->where('phone',$referrer)->first();

		if ($member) {

			return $member;

		} else {

			return false;

		}

	}



	/**

     * 获取一条数据 Home

     * @param array $where

     * @param  $select

     * @param bool $or

     * @return bool|Model|null|static

     */

    public function find_members($where = array() ,$select = '*',$or = null)

    {

        $table = $this->select($select)->where($where);

        if($or){

            $table = $table->orWhere($or);

        }

        $rico =  $table->first();

        if ($rico) {

            return $rico;

        } else {

            return false;

        }

    }

    /**

     * 获取所有数据

     * @param array $where

     * @param  $select

     * @param bool $or

     * @param  $orderBy

     * @param  $desc

     * @return bool|Model|null|static

     */

    public function get_members($where = array() ,$select = '*',$or = null ,$orderBy = 'uid',$desc = 'desc')

    {

        $table = $this->select($select)->where($where);

        if($orderBy){

            $table = $table->orderBy($orderBy,$desc);

        }

        if($or){

            $table = $table->orWhere($or);

        }

        $rico =  $table->get()->toArray();

        if ($rico) {

            return $rico;

        } else {

            return false;

        }

    }

    /**

     * 更新数据

     * @param array $where

     * @param array $data

     * @return bool

     */

    public function modification($where = array(),$data = array())

    {

        $rico = $this->where($where)->update($data);

        if ($rico) {

            return $rico;

        } else {

            return false;

        }

    }

    

     /**

     * @return array|string

     * 获取所有直商城会员

     */

    public function getStraightMembers(){

        $memberData = $this->where('straight_shop_isdown',1)->where('level','>=',1)->get()->toArray();

        if ($memberData){

            return $memberData;

        } else {

            return '没有数据';

        }

    }



    /**

     * @param $uid 会员老师id

     * @param $teamUid 分区团队会员uid

     * 查询上级V3会员

     */

    public function searchV3( $uid ,$teamUid )

    {

        $res = array();

        $member = $this->where('uid',$uid)->first();

        if ($member){

            if ($member['super_level'] == 3){

                $member['teamUid'] = $teamUid;

                $res['v3'] = $member;

                return $res;

            } else {

                $uid = $member['referrer'];

                $teamUid = $member['uid'];

                return $this->searchV3($uid,$teamUid);

            }

        } else {

            return false;

        }



    }



    /**

     * @param $userData

     * 查询V3会员下另外是否有V3会员

     */

    public function isContainV3( $userData = array() )

    {

        if (!empty($userData)){

            $array = $userData;

            foreach ($userData as $k => $v){

                if ($v['super_level'] == 3){

                    return true;

                } else {

                    unset($array[$k]);

                    $straight = $this->where('referrer',$v['uid'])->get()->toArray();

                    if ($straight){

                        array_merge($array,$straight);

                    }

                }

            }

            return $this->isContainV3($array);

        } else {

            return false;

        }



    }
    

}

